package cn.gene.timeyan.pay.webapi.service.impl;

import cn.gene.timeyan.common.exception.TimeyanServiceException;
import cn.gene.timeyan.common.restful.ResponseCode;
import cn.gene.timeyan.pay.service.ProductService;
import cn.gene.timeyan.pay.webapi.mapper.ProductMapper;
import cn.gene.timeyan.pojo.pay.model.Product;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Override
    public int queryStockById(Long id) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Product product = baseMapper.selectOne(queryWrapper);
        return product.getStock();
    }

    @Override
    public void updateStockById(Long id) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        int stock = this.queryStockById(id);
        queryWrapper.eq("id",id);
        Product product = new Product();
        product.setStock(stock-1);
        int row = baseMapper.update(product, queryWrapper);
        if (row != 1){
            throw new TimeyanServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"服务器繁忙!请稍后重试");
        }
    }

    @DS("timeyan_mall")
    @Override
    public int queryPriceById(Long id) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Product product = baseMapper.selectOne(queryWrapper);
        return product.getPrice();
    }
}
